package com.example.testtransition

import android.annotation.SuppressLint
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.parcel.Parcelize
import java.util.*

class MyAdapter : ListAdapter<MyAdapter.Album, MyAdapter.AlbumViewHolder>{

    var listener : AlbumAdapterListener? = null

    var itemLayout : Int = 0

    companion object {
        val DIFF_CALLBACK: DiffUtil.ItemCallback<Album> = object : DiffUtil.ItemCallback<Album>() {
            override fun areItemsTheSame(newItem: Album, oldItem: Album): Boolean {
                return newItem.title == oldItem.title
            }

            override fun areContentsTheSame(newItem: Album, oldItem: Album): Boolean {
                return false
            }
        }

        private val TRACKS: List<Track> =
            Arrays.asList(
                Track(1, "First", "3:25", true),
                Track(2, "Second", "4:51", false),
                Track(3, "Third", "4:12", false),
                Track(4, "Fourth", "2:34", false),
                Track(5, "Fifth", "439", false),
                Track(6, "Sixth", "2:31", false),
                Track(7, "Seventh", "5:25", false),
                Track(8, "Eighth", "3:46", false),
                Track(9, "Ninth", "4:28", false),
                Track(10, "Tenth", "4:47", false),
                Track(11, "Eleventh", "5:14", false),
                Track(12, "Twelfth", "4:46", false),
                Track(
                    13,
                    "Thirteenth",
                    "7:13",
                    false
                ),
                Track(14, "Fourteenth", "2:43", false)
            )


        val ALBUMS = Arrays.asList(
            Album(
                0L,
                "Metamorphosis",
                "Sandra Adams",
//                R.drawable.album_efe_kurnaz_unsplash,
                TRACKS,
                "52 mins"
            ),
            Album(
                1L,
                "Continuity",
                "Ali Connors",
//                R.drawable.album_pawel_czerwinski_unsplash,
                TRACKS,
                "92 mins"
            ),
            Album(
                2L,
                "Break Point",
                "David Park",
//                R.drawable.album_jean_philippe_delberghe_unsplash,
                TRACKS,
                "45 mins"
            ),
            Album(
                3L,
                "Headspace",
                "Charlie z.",
//                R.drawable.album_karina_vorozheeva_unsplash,
                TRACKS,
                "65 mins"
            ),
            Album(
                4L,
                "New Neighbors",
                "Trevor Hansen",
//                R.drawable.album_amy_shamblen_unsplash,
                TRACKS,
                "73 mins"
            ),
            Album(
                5L,
                "Spaced Out",
                "Jonas Eckhart",
//                R.drawable.album_pawel_czerwinski_unsplash_2,
                TRACKS,
                "4 mins"
            ),
            Album(
                6L,
                "Holding on",
                "Elizabeth Park",
//                R.drawable.album_kristopher_roller_unsplash,
                TRACKS,
                "40 mins"
            ),
            Album(
                7L,
                "Persistence",
                "Britta Holt",
//                R.drawable.album_emile_seguin_unsplash,
                TRACKS,
                "39 mins"
            ),
            Album(
                8L,
                "At the Top",
                "Annie Chiu",
//                R.drawable.album_ellen_qin_unsplash,
                TRACKS,
                "46 mins"
            ),
            Album(
                9L,
                "On Dry Land",
                "Alfonso Gonzalez",
//                R.drawable.album_david_clode_unsplash,
                TRACKS,
                "55 mins"
            )
        )

    }

    constructor(listener: AlbumAdapterListener)
    : super(DIFF_CALLBACK){
        this.listener = listener
        this.itemLayout = R.layout.moment_card_header
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i : Int) : AlbumViewHolder {
        return AlbumViewHolder(LayoutInflater.from(viewGroup.context).inflate(itemLayout, viewGroup, false), listener)
    }

    override fun onBindViewHolder(viewHolder: AlbumViewHolder, i : Int) {
        viewHolder.bind(getItem(i))
    }


    /** A data class to hold information about a music album.  */
    @SuppressLint("ParcelCreator")// 用于处理 Lint 的错误提示
    @Parcelize
    class Album(
        val id: Long, val
        title: String,
        val artist: String,
//        @field:DrawableRes @param:DrawableRes val cover: Int,
       var tracks: List<Track>,
        var duration: String) : Parcelable {
//        val tracks: List<Track>
//        val duration: String

        companion object {
            val DIFF_CALLBACK: DiffUtil.ItemCallback<Album> =
                object : DiffUtil.ItemCallback<Album>() {
                    override fun areItemsTheSame(newItem: Album, oldItem: Album): Boolean {
                        return newItem.title == oldItem.title
                    }

                    override fun areContentsTheSame(newItem: Album, oldItem: Album): Boolean {
                        return false
                    }
                }
        }

//        init {
//            this.tracks = tracks
//            this.duration = duration
//        }

        override fun toString(): String {
            return "id =$id, title = $title, artist = $artist, tracks = $tracks, duration = $duration"
        }
    }

    /** A data class to hold information about a track on an album.  */
    @SuppressLint("ParcelCreator")// 用于处理 Lint 的错误提示
    @Parcelize
    class Track(
        val track: Int,
        val title: String,
        val duration: String,
        var playing: Boolean
    ): Parcelable  {


        companion object {
            val DIFF_CALLBACK: DiffUtil.ItemCallback<Track> =
                object : DiffUtil.ItemCallback<Track>() {
                    override fun areItemsTheSame(newItem: Track, oldItem: Track): Boolean {
                        return newItem.title == oldItem.title
                    }

                    override fun areContentsTheSame(newItem: Track, oldItem: Track): Boolean {
                        return false
                    }
                }
        }

        override fun toString(): String {
//            return super.toString()
            return "track = $track, title = $title, duration = $duration, playing = $playing"
        }
    }


    interface AlbumAdapterListener {
        fun onAlbumClicked(view: View?, album: Album?)
    }

    class AlbumViewHolder(
        view: View,
        listener:  AlbumAdapterListener?) : RecyclerView.ViewHolder(view) {
        private val listener: AlbumAdapterListener?
        private val container: View
        private val albumImage: ImageView
        private val albumTitle: TextView
        private val albumArtist: TextView

        // Optional field available only in the list item layout.
        private val albumDuration: TextView?
        fun bind(album: Album) {
            // Use a unique transition name so this item can be used as a shared element when
            // transitioning to the album details screen.
            ViewCompat.setTransitionName(container, album.title)
            container.setOnClickListener { v: View? ->
                listener?.onAlbumClicked(
                    container,
                    album
                )
            }
//            albumImage.setImageResource(album.cover)
            albumTitle.text = album.title
//            albumArtist.text = album.artist
            if (albumDuration != null) {
                albumDuration.text = album.duration
            }
        }

        init {
            this.listener = listener
            container = view.findViewById(R.id.container)
            albumImage = view.findViewById(R.id.img_avatar)
            albumTitle = view.findViewById(R.id.text_nickname)
            albumArtist = view.findViewById(R.id.text_content)
            albumDuration = view.findViewById(R.id.tv_moment_item_date)
        }
    }
}